﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThongTinNhanKhau.model.BO
{
    class TachHoKhauBO
    {
        model.DAO.TachHoKhauDAO tachHoKhauDAO = new DAO.TachHoKhauDAO();
        GUI.TachHoKhau f;
        GUI.HoKhau hoKhauDesign;

        public TachHoKhauBO(GUI.TachHoKhau tachHoKhau, GUI.HoKhau hoKhau)
        {
            this.f = tachHoKhau;
            hoKhauDesign = hoKhau;
        }

        internal void loadLeftField(string maHoKhau, string maNhanKhau)
        {
            DataTable dt = tachHoKhauDAO.loadLeftField(maNhanKhau);

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    this.f.tbHoTen.Text = dr["HoTen"].ToString();
                    this.f.tbCMND.Text = dr["CMND"].ToString();

                    DateTime dtime = DateTime.Parse(dr["NgaySinh"].ToString());
                    this.f.tbNgaySinh.Text = dtime.ToShortDateString();
                }
            }

            this.f.tbMaNhanKhau.Text = maNhanKhau;
            this.f.tbMaHoKhauHienTai.Text = maHoKhau;
        }

        internal void loadHoKhau(String maHoKhau)
        {
            DataTable dt = tachHoKhauDAO.loadHoKhau(maHoKhau);

            if (dt.Rows.Count != 0)
            {
                this.f.gridControlHoKhau.DataSource = dt;
            }
            else
            {
                this.f.gridControlHoKhau.DataSource = null;
            }
        }

        internal void btnTimKiem()
        {
            DataTable dt = tachHoKhauDAO.btnTimKiem(this.f.tbFindMaHoKhau.Text.Trim(), this.f.tbFindTenChuHo.Text.Trim(), this.f.tbMaHoKhauHienTai.Text.Trim());

            if (dt.Rows.Count != 0)
            {
                this.f.gridControlHoKhau.DataSource = dt;
            }
            else
            {
                this.f.gridControlHoKhau.DataSource = null;
            }
        }

        internal void btnLuu()
        {
            String maHoKhauMoi = this.f.tbSelectMaHoKhau.Text;
            String maNhanKhau = this.f.tbMaNhanKhau.Text;
            String quanHeChuHo = this.f.cboSelectQHCH.SelectedItem.ToString();

            if (tachHoKhauDAO.btnLuu(maHoKhauMoi, maNhanKhau, quanHeChuHo))
            {
                System.Windows.Forms.MessageBox.Show("Tách hộ khẩu thành công");
                hoKhauDesign.loadNhanKhau(this.f.tbMaHoKhauHienTai.Text);
            }
        }
    }
}
